<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/examples/activeqt/hierarchy.qdoc -->
<head>
  <title>Qt 4.3: Hierarchy Example (ActiveQt)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Hierarchy Example (ActiveQt)<br /><small></small></h1>
<p>Files:</p>
<ul>
<li><a href="activeqt-hierarchy-objects-cpp.html">activeqt/hierarchy/objects.cpp</a></li>
<li><a href="activeqt-hierarchy-objects-h.html">activeqt/hierarchy/objects.h</a></li>
<li><a href="activeqt-hierarchy-main-cpp.html">activeqt/hierarchy/main.cpp</a></li>
</ul>
<p>The Hierarchy example is shows how to write an in-process ActiveX control. The control is a <a href="qwidget.html">QWidget</a> subclass with child widgets that are accessible as sub-types.</p>
<pre> class QParentWidget : public QWidget
 {
     Q_OBJECT
     Q_CLASSINFO(&quot;ClassID&quot;, &quot;{d574a747-8016-46db-a07c-b2b4854ee75c}&quot;);
     Q_CLASSINFO(&quot;InterfaceID&quot;, &quot;{4a30719d-d9c2-4659-9d16-67378209f822}&quot;);
 public:
     QParentWidget(QWidget *parent = 0);

     QSize sizeHint() const;

 public slots:
     void createSubWidget( const QString &amp;name );

     QSubWidget *subWidget( const QString &amp;name );

 private:
     QVBoxLayout *vbox;
 };</pre>
<p>The <tt>QParentWidget</tt> class provides slots to create a widget with a name, and to return a pointer to a named widget. The class declaration uses <tt>Q_CLASSINFO()</tt> to provide the COM identifiers for this class.</p>
<pre> QParentWidget::QParentWidget(QWidget *parent)
 : QWidget(parent)
 {
     vbox = new QVBoxLayout(this);
 }</pre>
<p>The constructor of QParentWidget creates a vertical box layout. New child widgets are automatically added to the layout.</p>
<pre> void QParentWidget::createSubWidget(const QString &amp;name)
 {
     QSubWidget *sw = new QSubWidget(this, name);
     vbox-&gt;addWidget(sw);
     sw-&gt;setLabel(name);
     sw-&gt;show();
 }</pre>
<p>The <tt>createSubWidget</tt> slot creates a new <tt>QSubWidget</tt> with the name provided in the parameter, and sets the label to that name. The widget is also shown explicitly.</p>
<pre> QSubWidget *QParentWidget::subWidget(const QString &amp;name)
 {
     return qFindChild&lt;QSubWidget*&gt;(this, name);
 }</pre>
<p>The <tt>subWidget</tt> slot uses the <tt>QObject::child()</tt> function and returns the first child of type <tt>QSubWidget</tt> that has the requested name.</p>
<pre> class QSubWidget : public QWidget
 {
     Q_OBJECT
     Q_PROPERTY( QString label READ label WRITE setLabel )

     Q_CLASSINFO(&quot;ClassID&quot;, &quot;{850652f4-8f71-4f69-b745-bce241ccdc30}&quot;);
     Q_CLASSINFO(&quot;InterfaceID&quot;, &quot;{2d76cc2f-3488-417a-83d6-debff88b3c3f}&quot;);
     Q_CLASSINFO(&quot;ToSuperClass&quot;, &quot;QSubWidget&quot;);

 public:
     QSubWidget(QWidget *parent = 0, const QString &amp;name = QString());

     void setLabel( const QString &amp;text );
     QString label() const;

     QSize sizeHint() const;

 protected:
     void paintEvent( QPaintEvent *e );

 private:
     QString lbl;
 };</pre>
<p>The <tt>QSubWidget</tt> class has a single string-property <tt>label</tt>, and implements the paintEvent to draw the label. The class uses again <tt>Q_CLASSINFO</tt> to provide the COM identifiers, and also sets the <i>ToSuperClass</i> attribute to <i>QSubWidget</i>, to ensure that only no slots of any superclasses (i.e&#x2e; <a href="qwidget.html">QWidget</a>) are exposed.</p>
<pre> QSubWidget::QSubWidget(QWidget *parent, const QString &amp;name)
 : QWidget(parent)
 {
     setObjectName(name);
 }

 void QSubWidget::setLabel(const QString &amp;text)
 {
     lbl = text;
     setObjectName(text);
     update();
 }

 QString QSubWidget::label() const
 {
     return lbl;
 }

 QSize QSubWidget::sizeHint() const
 {
     QFontMetrics fm(font());
     return QSize(fm.width(lbl), fm.height());
 }

 void QSubWidget::paintEvent(QPaintEvent *)
 {
     QPainter painter(this);
     painter.setPen(palette().text().color());
     painter.drawText(rect(), Qt::AlignCenter, lbl);
 }</pre>
<p>The implementation of the QSubWidget class is self-explanatory.</p>
<pre> #include &quot;objects.h&quot;
 #include &lt;QAxFactory&gt;

 QAXFACTORY_BEGIN(&quot;{9e626211-be62-4d18-9483-9419358fbb03}&quot;, &quot;{75c276de-1df5-451f-a004-e4fa1a587df1}&quot;)
     QAXCLASS(QParentWidget)
     QAXTYPE(QSubWidget)
 QAXFACTORY_END()</pre>
<p>The classes are then exported using a <a href="qaxfactory.html">QAxFactory</a>. <tt>QParentWidget</tt> is exported as a full class (which can be created ), while <tt>QSubWidget</tt> is only exported as a type, which can only be created indirectly through APIs of <tt>QParentWidget</tt>.</p>
<p>To build the example you must first build the <a href="qaxserver.html">QAxServer</a> library. Then run qmake and your make tool in <tt>examples/activeqt/multiple</tt>.</p>
<p>The <a href="qaxserver-demo-hierarchy.html">demonstration</a> requires your WebBrowser to support ActiveX controls, and scripting to be enabled.</p>
<pre> &lt;script language=&quot;javascript&quot;&gt;
 function createSubWidget( form )
 {
     ParentWidget.createSubWidget( form.nameEdit.value );
 }

 function renameSubWidget( form )
 {
     var SubWidget = ParentWidget.subWidget( form.nameEdit.value );
     if ( !SubWidget ) {
         alert( &quot;No such widget &quot; + form.nameEdit.value + &quot;!&quot; );
         return;
     }
     SubWidget.label = form.labelEdit.value;
     form.nameEdit.value = SubWidget.label;
 }

 function setFont( form )
 {
     ParentWidget.font = form.fontEdit.value;
 }
 &lt;/script&gt;

 &lt;p&gt;
 This widget can have many children!
 &lt;/p&gt;
 &lt;object ID=&quot;ParentWidget&quot; CLASSID=&quot;CLSID:d574a747-8016-46db-a07c-b2b4854ee75c&quot;
 CODEBASE=&quot;http://www.trolltech.com/demos/hierarchy.cab&quot;&gt;
 [Object not available! Did you forget to build and register the server?]
 &lt;/object&gt;&lt;br /&gt;
 &lt;form&gt;
 &lt;input type=&quot;edit&quot; ID=&quot;nameEdit&quot; value=&quot;&amp;lt;enter object name&amp;gt;&quot; /&gt;
 &lt;input type=&quot;button&quot; value=&quot;Create&quot; onClick=&quot;createSubWidget(this.form)&quot; /&gt;
 &lt;input type=&quot;edit&quot; ID=&quot;labelEdit&quot; /&gt;
 &lt;input type=&quot;button&quot; value=&quot;Rename&quot; onClick=&quot;renameSubWidget(this.form)&quot; /&gt;
 &lt;br /&gt;
 &lt;input type=&quot;edit&quot; ID=&quot;fontEdit&quot; value=&quot;MS Sans Serif&quot; /&gt;
 &lt;input type=&quot;button&quot; value = &quot;Set Font&quot; onClick=&quot;setFont(this.form)&quot; /&gt;
 &lt;/form&gt;</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
